<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>Document</title>
  <link rel="stylesheet" href="css/style.css" />
  <script type="text/javascript" src="js/jquery.min.js"></script>
  <script type="text/javascript">
    (function ($) {
      var printAreaCount = 0;
      $.fn.printArea = function () {
        var ele = $(this);
        var idPrefix = "printArea_";
        removePrintArea(idPrefix + printAreaCount);
        printAreaCount++;
        var iframeId = idPrefix + printAreaCount;
        var iframeStyle = 'position:absolute;width:0px;height:0px;left:-500px;top:-500px;';
        iframe = document.createElement('IFRAME');
        $(iframe).attr({
          style: iframeStyle,
          id: iframeId
        });
        document.body.appendChild(iframe);
        var doc = iframe.contentWindow.document;
        $(document).find("link").filter(function () {
          return $(this).attr("rel").toLowerCase() == "stylesheet";
        }).each(
          function () {
            doc.write('<link type="text/css" rel="stylesheet" href="' +
              $(this).attr("href") + '" >');
          });
        doc.write('<div class="' + $(ele).attr("class") + '">' + $(ele).html() +
          '</div>');
        doc.close();
        var frameWindow = iframe.contentWindow;
        frameWindow.close();
        frameWindow.focus();
        frameWindow.print();
      }
      var removePrintArea = function (id) {
        $("iframe#" + id).remove();
      };
    })(jQuery);
  </script>
</head>

<body>
  <div>
    <div id="print-area">
      <table>
        <tr>
          <td class="dd">test</td>
          <td>test</td>
          <td>test</td>
          <td>test</td>
          <td>test</td>
        </tr>
        <tr>
          <td>test</td>
          <td>test</td>
          <td>test</td>
          <td>test</td>
          <td>test</td>
        </tr>
        <tr>
          <td>test</td>
          <td>test</td>
          <td>test</td>
          <td>test</td>
          <td>test</td>
        </tr>
        <tr>
          <td>test</td>
          <td>test</td>
          <td>test</td>
          <td>test</td>
          <td>test</td>
        </tr>
        <tr>
          <td>test</td>
          <td>test</td>
          <td>test</td>
          <td>test</td>
          <td>test</td>
        </tr>
      </table>
    </div>
    <input type="button" onclick=" print()" value="打印">

  </div>
</body>
<script type="text/javascript">
  $(function () {
    $("#btnPrint").click(function () {
      $("#print-area").printArea();
    });
  });
</script>

</html>